/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.oea.servlets;

import com.oea.hibernate.entities.Branch;
import com.oea.hibernate.entities.Division;
import com.oea.hibernate.entities.Exam;
import com.oea.hibernate.entities.Faculty;
import com.oea.hibernate.entities.Question;
import com.oea.hibernate.entities.Result;
import com.oea.hibernate.entities.Student;
import com.oea.hibernate.entities.Subject;
import com.oea.hibernate.entities.Test;
import com.oea.hibernate.entities.Topic;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author unnati
 */
public class PageAction extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */

            int pageid = Integer.parseInt(request.getParameter("pageid").trim());
            String action = request.getParameter("action");

            int begin = (pageid - 1) * 10;
            int end = begin + 9;
            double pagelink;

            if (action.equals("faculty")) {
                List<Faculty> li_allfaculties = (List<Faculty>) request.getServletContext().getAttribute("li_allfaculties");

                pagelink = Math.ceil((float) li_allfaculties.size() / 10);
                System.out.println(pagelink);

                if (li_allfaculties.size() <= end) {
                    end = li_allfaculties.size() - 1;
                }

                List<Faculty> page_li_allfacilties = new ArrayList<>();
                for (int i = begin; i <= end; i++) {
                    page_li_allfacilties.add(li_allfaculties.get(i));

                }
                

                request.getSession().setAttribute("page_li_allfacilties", page_li_allfacilties);
                request.getSession().setAttribute("pagelink", pagelink);

                response.sendRedirect("view-faculties.jsp");

            } else if (action.equals("student")) {
                List<Student> li_allstudents = (List<Student>) request.getSession().getAttribute("li_allstudents");
                
                pagelink = Math.ceil((float) li_allstudents.size() / 10);

                if (li_allstudents.size() <= end) {
                    end = li_allstudents.size() - 1;
                }

                List<Student> page_li_allstudents = new ArrayList<>();
                for (int i = begin; i <= end; i++) {
                    page_li_allstudents.add(li_allstudents.get(i));

                }
                for (Student student : page_li_allstudents) {
                    student.setAdmin();
                }

                request.getSession().setAttribute("page_li_allstudents", page_li_allstudents);
                request.getSession().setAttribute("pagelink", pagelink);

                response.sendRedirect("view-student.jsp");

            } else if (action.equals("division")) {

                List<Division> li_alldivisions = (List<Division>) request.getServletContext().getAttribute("li_alldivisions");
                
                pagelink = Math.ceil((float) li_alldivisions.size() / 10);

                if (li_alldivisions.size() <= end) {
                    end = li_alldivisions.size() - 1;
                }

                List<Division> page_li_alldivisions = new ArrayList<>();
                for (int i = begin; i <= end; i++) {
                    page_li_alldivisions.add(li_alldivisions.get(i));

                }
                for (Division division : page_li_alldivisions) {
                    division.setAdmin();
                    division.setBranch();

                }

                request.getServletContext().setAttribute("page_li_alldivisions", page_li_alldivisions);
                request.getSession().setAttribute("pagelink", pagelink);

                response.sendRedirect("view-division.jsp");

            } else if (action.equals("branch")) {

                List<Branch> li_allbranches = (List<Branch>) request.getServletContext().getAttribute("li_allbranches");

                pagelink = Math.ceil((float) li_allbranches.size() / 10);

                if (li_allbranches.size() <= end) {
                    end = li_allbranches.size() - 1;
                }

                List<Branch> page_li_allbranches = new ArrayList<>();
                for (int i = begin; i <= end; i++) {
                    page_li_allbranches.add(li_allbranches.get(i));

                }
                for (Branch branch : page_li_allbranches) {
                    branch.setAdmin();

                }

                request.getServletContext().setAttribute("page_li_allbranches", page_li_allbranches);
                request.getSession().setAttribute("pagelink", pagelink);

                response.sendRedirect("view-branch.jsp");

            } else if (action.equals("subject")) {

                List<Subject> li_allsubjects = (List<Subject>) request.getServletContext().getAttribute("li_allsubjects");

                pagelink = Math.ceil((float) li_allsubjects.size() / 10);

                if (li_allsubjects.size() <= end) {
                    end = li_allsubjects.size() - 1;
                }

                List<Subject> page_li_allsubjects = new ArrayList<>();
                for (int i = begin; i <= end; i++) {
                    page_li_allsubjects.add(li_allsubjects.get(i));

                }
                for (Subject subject : page_li_allsubjects) {
                    subject.setAdmin();

                }

                request.getServletContext().setAttribute("page_li_allsubjects", page_li_allsubjects);
                request.getSession().setAttribute("pagelink", pagelink);

                response.sendRedirect("view-subject.jsp");

            } else if (action.equals("result")) {

                List<Result> li_allresults = (List<Result>) request.getSession().getAttribute("li_result");

                pagelink = Math.ceil((float) li_allresults.size() / 10);

                if (li_allresults.size() <= end) {
                    end = li_allresults.size() - 1;
                }

                List<Result> page_li_allresults = new ArrayList<>();
                for (int i = begin; i <= end; i++) {
                    page_li_allresults.add(li_allresults.get(i));

                }
                for (Result result : page_li_allresults) {
                    result.setStudent();
                    result.setTest();

                }

                request.getSession().setAttribute("page_li_result", page_li_allresults);
                request.getSession().setAttribute("pagelink", pagelink);

                response.sendRedirect("view-result.jsp");

            } else if (action.equals("question")) {

                List<Question> li_allquestions = (List<Question>) request.getSession().getAttribute("li_allquestions");

                pagelink = Math.ceil((float) li_allquestions.size() / 10);

                if (li_allquestions.size() <= end) {
                    end = li_allquestions.size() - 1;
                }

                List<Question> page_li_allquestions = new ArrayList<>();
                for (int i = begin; i <= end; i++) {
                    page_li_allquestions.add(li_allquestions.get(i));

                }
                for (Question question : page_li_allquestions) {
                    question.setTopic();

                }

                request.getSession().setAttribute("page_li_allquestions", page_li_allquestions);
                request.getSession().setAttribute("pagelink", pagelink);

                response.sendRedirect("view-question.jsp");
            } else if (action.equals("test")) {

                List<Test> li_alltests = (List<Test>) request.getSession().getAttribute("li_alltests");

                pagelink = Math.ceil((float) li_alltests.size() / 10);

                if (li_alltests.size() <= end) {
                    end = li_alltests.size() - 1;
                }

                List<Test> page_li_alltests = new ArrayList<>();
                for (int i = begin; i <= end; i++) {
                    page_li_alltests.add(li_alltests.get(i));

                }
                for (Test test : page_li_alltests) {
                    test.setFaculty();
                    test.setExam();
                    test.setSubject();
                }

                request.getSession().setAttribute("page_li_alltests", page_li_alltests);
                request.getSession().setAttribute("pagelink", pagelink);

                response.sendRedirect("view-test.jsp");
            } else if (action.equals("exam")) {

                List<Exam> li_allexams = (List<Exam>) request.getSession().getAttribute("li_allexams");

                pagelink = Math.ceil((float) li_allexams.size() / 10);

                if (li_allexams.size() <= end) {
                    end = li_allexams.size() - 1;
                }

                List<Exam> page_li_allexams = new ArrayList<>();
                for (int i = begin; i <= end; i++) {
                    page_li_allexams.add(li_allexams.get(i));

                }
                for (Exam exam : page_li_allexams) {
                    exam.setFaculty();
                    exam.setBranch();
                }

                request.getSession().setAttribute("page_li_allexams", page_li_allexams);
                request.getSession().setAttribute("pagelink", pagelink);

                response.sendRedirect("view-exam.jsp");
            } else if (action.equals("topic")) {

                List<Topic> li_alltopics = (List<Topic>) request.getSession().getAttribute("li_alltopics");

                pagelink = Math.ceil((float) li_alltopics.size() / 10);

                if (li_alltopics.size() <= end) {
                    end = li_alltopics.size() - 1;
                }

                List<Topic> page_li_alltopics = new ArrayList<>();
                for (int i = begin; i <= end; i++) {
                    page_li_alltopics.add(li_alltopics.get(i));

                }
                for (Topic topic : page_li_alltopics) {
                    topic.setSubject();
                }

                request.getSession().setAttribute("page_li_alltopics", page_li_alltopics);
                request.getSession().setAttribute("pagelink", pagelink);

                response.sendRedirect("view-topic.jsp");
            } else if (action.equals("test_question")) {

                List<Question> list_question_test = null;
                if (request.getSession().getAttribute("go") != null) {
                    list_question_test = (List<Question>) request.getSession().getAttribute("li_question_by_topic");
                } else {
                    list_question_test = (List<Question>) request.getSession().getAttribute("list_question_test");
                }

                pagelink = Math.ceil((float) list_question_test.size() / 10);

                if (list_question_test.size() <= end) {
                    end = list_question_test.size() - 1;
                }

                List<Question> page_list_question_test = new ArrayList<>();
                for (int i = begin; i <= end; i++) {
                    page_list_question_test.add(list_question_test.get(i));

                }
                for (Question q : page_list_question_test) {
                    q.setTopic();
                }

                request.getSession().setAttribute("page_list_question_test", page_list_question_test);
                request.getSession().setAttribute("pagelink", pagelink);

                response.sendRedirect("test-questions.jsp");
            } else if (action.equals("added_test_question")) {

                List<Question> list_question_test = null;
                if (request.getSession().getAttribute("added_go") != null) {
                    list_question_test = (List<Question>) request.getSession().getAttribute("li_question_by_topic");
                } else {
                    list_question_test = (List<Question>) request.getSession().getAttribute("li_added_test_question");
                }

                pagelink = Math.ceil((float) list_question_test.size() / 10);

                if (list_question_test.size() <= end) {
                    end = list_question_test.size() - 1;
                }

                List<Question> page_list_question_test = new ArrayList<>();
                for (int i = begin; i <= end; i++) {
                    page_list_question_test.add(list_question_test.get(i));

                }
                for (Question q : page_list_question_test) {
                    q.setTopic();
                }

                request.getSession().setAttribute("page_list_question_test", page_list_question_test);
                request.getSession().setAttribute("pagelink", pagelink);

                response.sendRedirect("added_test-questions.jsp");
            }

        }
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
